﻿@using SmartSiteManagementSystem.Models.Admin;
@using SmartSiteManagementSystem.IServices;
@inject IAdminService _adminService
@{
    Layout = "~/Views/Shared/_Layout.cshtml";
    ViewBag.BodyClass = "easyui-layout";
    AdminUserInfo adminUser = ViewBag.adminUser;
    IList<AdminGroupInfo> adminGroupList = ViewBag.adminGroupList;
}
    <div class="easyui-panel" style="padding:5px;">    
        <input id="AdminUserId" type="hidden" value="@adminUser.AdminUserId" />
        <table class="gou-formTable" style="align-content:center;">
            <tbody>              
                <tr>
                    <td align="right" class="gou-formTable-title" style="width:160px;">*用户名</td>
                    <td align="left" class="">

    <input id="UserName" name="UserName" type="text" value="@adminUser.UserName" @if(adminUser.AdminUserId>0){@Html.Raw(" readonly=readonly")} />    
                    </td>
                </tr>
                <tr>
                    <td align="right" class="gou-formTable-title">*密码</td>
                    <td align="left" class="">
                        <input id="UserPwd" name="UserPwd" type="password" value="@adminUser.UserPwd" /> 
                    </td>
                </tr>                      
                <tr>
                    <td align="right" class="gou-formTable-title">邮箱</td>
                    <td align="left" class="">                        
                        <input id="Email" name="Email" type="text" value="@adminUser.Email" /> 
                    </td>
                </tr>

                <tr>
                    <td align="right" class="gou-formTable-title">手机号</td>
                    <td align="left" class="">                        
                        <input id="HandMobile" name="HandMobile" type="text" value="@adminUser.HandMobile" />                      
                      
                    </td>
                </tr>                
               
                <tr>
                    <td align="right" class="gou-formTable-title">角色组</td>
                    <td align="left" class="">                        
                        @foreach (var item in adminGroupList) { 
                         
                            <label style="width:120px; display:inline-block;">
			                    <input type="checkbox" name="ck_usergroup" value="@item.AdminUserGroupId" @if(_adminService.IsExistAdminUserGroupRelation(item.AdminUserGroupId,adminUser.AdminUserId)){ @Html.Raw("checked=checked");} />@item.AdminUserGroupName
                            </label>                            
                        }                      

                    </td>
                </tr>

                <tr>
                    <td align="right" class="gou-formTable-title">备注</td>
                    <td align="left" class="">
                        <input id="Remark" name="Remark" type="text" value="@adminUser.Remark" />

                    </td>
                </tr>

                <tr>
                    <td colspan="2" align="center">                       
                            <a href="javascript:void(0)" id="hfSave" class="easyui-linkbutton" iconcls="icon-save" onclick="SaveAdminUser();" style="width:150px;height:32px">保存</a>                                           
                    </td>
                </tr>
            </tbody>
        </table>

    </div>



    <script type="text/javascript">        

        function ValidateEmail(data) {
            return true;
            @*var emailreg = /\w+[@]{1}\w+[.]\w+/;

            if (!emailreg.test(data)) {
                return true;
            } else {
                return false;
            }*@
        }
        function ValidateSecIdCard(n) {
            var d = 0;
            var l = n;
            var i = { 11: "北京", 12: "天津", 13: "河北", 14: "山西", 15: "内蒙", 21: "辽宁", 22: "吉林", 23: "黑龙", 31: "上海", 32: "江苏", 33: "浙江", 34: "安徽", 35: "福建", 36: "江西", 37: "山东", 41: "河南", 42: "湖北", 43: "湖南", 44: "广东", 45: "广西", 46: "海南", 50: "重庆", 51: "四川", 52: "贵州", 53: "云南", 54: "西藏", 61: "陕西", 62: "甘肃", 63: "青海", 64: "宁夏", 65: "新疆", 71: "台湾", 81: "香港", 82: "澳门", 91: "国外" };
            if (!/^\d{17}(\d|x)$/i.test(l)) {
                return false;
            }
            l = l.replace(/x$/i, "a");
            if (i[parseInt(l.substr(0, 2))] == null) {
                return false;
            }
            var j = l.substr(6, 4) + "-" + Number(l.substr(10, 2)) + "-" + Number(l.substr(12, 2));
            var m = new Date(j.replace(/-/g, "/"));
            if (j != (m.getFullYear() + "-" + (m.getMonth() + 1) + "-" + m.getDate())) {
                return false;
            }
            for (var k = 17; k >= 0; k--) {
                d += (Math.pow(2, k) % 11) * parseInt(l.charAt(17 - k), 11)
            }
            if (d % 11 != 1) {
                return false;
            }
            return true;
        }

        function GetAdminGroupIds() {
            var ids = "";
            $('input[name="ck_usergroup"]:checked').each(function () {                
                if (ids != '') ids += ',';
                ids += $(this).val();
            });
            return ids;
        }

        //保存
        function SaveAdminUser() {

            //验证必选项******************************************************************
            var UserName = $("#UserName").val();
            var UserPwd = $("#UserPwd").val();
            var UserType = 1;
            var Email = $("#Email").val();
            var IdCard = $("#IdCard").val();
            var hidIdCard = $("#hidIdCard").val();
            var HandMobile = $("#HandMobile").val();
            var Remark = $("#Remark").val();
            var AdminUserId = $("#AdminUserId").val();
            var AdminGroupIds = "";

            if (UserName == "") {
                $.messager.alert('提示', '用户名为必填项!');
                return false;
            }
            if (UserPwd == "") {
                $.messager.alert('提示', '密码为必填项!');
                return false;
            }
          
            if (Email != "") {
                if (!ValidateEmail(Email)) {
                    $.messager.alert('提示', '请填写合法邮箱!');
                    return false;
                }
            }

            //if (IdCard != "") {
            //    if (!ValidateSecIdCard(IdCard)) {
            //        $.messager.alert('提示', '请填写合法身份证号码!');
            //        return false;
            //    }
            //}

            AdminGroupIds = GetAdminGroupIds();
            
            if (AdminGroupIds == "" || AdminGroupIds == undefined || AdminGroupIds == null) {
                $.messager.alert('提示', '请选择角色!');
                return false;
            }            
            
            var param = { AdminUserId: AdminUserId, UserName: UserName, UserPwd: UserPwd, UserType: UserType, Email: Email, IdCard: IdCard, HandMobile: HandMobile, Remark: Remark, AdminGroupIds: AdminGroupIds, oldIdCard: hidIdCard };
            $.ajax({
                "type": "Post",
                "url": "ajaxAccess?t=" + new Date().getTime(),
                "dataType": "json",
                "data": param,
                "success": function (data) {
                    if (data.Code == 1) {
                        window.location.href = "index";
                    }
                    else if (data.Code == -2)
                    {
                        $.messager.alert('提示', '请填写合法身份证号码!');
                    }
                    else
                    {
                        $.messager.alert('提示', '保存失败');
                    }
                }
            });
        }
</script>    
